<template>
	<view class="warp">
		<navigator url="/pages/book/book_buy_list">
			<view class="read_log">兑换学习记录入口</view>
		</navigator>
		
		<unicloud-db ref="readRef" v-slot:default="{data, loading, error, options}" collection="wolf-read" orderby="sort" getcount @load="loadData">
			<view v-if="error">{{error.message}}</view>
			<view v-else>
				<uni-list>
					<uni-list-item v-for="(item,index) in data" :key="item._id">
						<template v-slot:body>
							<view class="list">
								<view class="info">
									<image :src="item.icon" mode="aspectFill" class="info-image"></image>
									<view class="info-title">
										<text>{{item.title}}</text> 
										<text style="font-size: 12px;color: #ff0000;"> [{{item.price}}学习分]</text>
									</view>
								</view>
								<view class="view" @click="read(item._id,item.price)">学习</view>
							</view>
						</template>
					</uni-list-item>
				</uni-list>
				<uni-load-more :status="more"></uni-load-more>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	const db = uniCloud.database();
	import {
		mapState,
		mapActions
	} from 'pinia'
	import {
		useUserStore
	} from "@/pinia/user.js"
	const readProfit = uniCloud.importObject('two-plus-one')
	
	
	export default {
		name:"wolf-share-header",
		data() {
			return {
				more:"more",
				readList:[],
			}
		},
		computed: {
			...mapState(useUserStore, ['userInfo', 'sysSetting']),
		},
		onLoad() {
			// this.load()
		},
		methods: {
			...mapActions(useUserStore, ['updateUserInfo']),
			//加载具备阅读权限的数据
			// async load(){
			// 	let res = await db.collection('wolf-reading-log').where(`user_id=='${this.userInfo._id}'`).field('_id').get()
			// 	this.readList = res.result.data.map(item=>item._id)
			// },
			loadData(data,e){
				if(e){
					this.more = "no-more"
				}else{
					this.more = "more"
				}
			},
			loadMore(){
				this.$refs.readRef.loadMore()
			},
			async read(id,price){
				let _that = this
				console.log("====",id,price);
				// if(this.readList.includes(id)){ //如果具有有效阅读的书籍，直接跳转阅读
				// 	return _that.navigator(id)
				// }
				if(price > this.userInfo.sign_score){
					return uni.showModal({
						title:"温馨提示",
						content:"1,学习分不够，坚持读书打卡;2,返回首页去服务套餐购买"
					})
				}
				uni.showModal({
					confirmText:"确认",
					content:`本次学习需要抵扣${price} 学习分`,
					title:"提示",
					success(res){
						if(res.confirm){
							console.log("确认阅读");
							_that.goRead(id,price)
						}
					}
				})
			},
			async goRead(id,price){
				console.log("====>",id,price);
				//更新后台学习分的分润，id应该是人，不是书籍ID 
				readProfit.setNewBookProfit({
					id:this.userInfo._id,
					price,
					orderid:'',
				})
				
				
				this.updateUserInfo({
					sign_score:this.userInfo.sign_score - price
				})
				await this.upReadPes(id)//更新具备阅读权限的人
				
				this.navigator(id)
			},
			async upReadPes(book_id){
				await db.collection('wolf-reading-log').add({
					user_buy_id:this.userInfo._id,
					user_id:this.userInfo._id,
					book_id:book_id,
					is_ok:1
				})
			},
			navigator(id){
				uni.navigateTo({
					url:`/pages/book/reading?id=${id}`
				})
			}
		},
		onReachBottom() {
			if(this.more == "more"){
				this.more = "loading"
				this.loadMore()
			}
		}
	}
</script>

<style scoped lang="scss">
	view{
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
	}
	.read_log{
		margin: 10rpx auto;
		width: 300rpx;
		text-align: center;
		padding: 10rpx;
		border-radius: 10rpx;
		background-color: #ea6cf5;
	}
	.list{
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		flex:1;
		
		.info{
			flex-direction: row;
			&-image{
				width: 50rpx;
				height: 50rpx;
				border-radius: 10rpx;
				margin-right: 10rpx;
			}
			&-title{
				font-weight: 700;
				flex-direction: row;
				align-items: baseline;
				padding-left: 10rpx;
			}
		}
		
		.view{
			background-color: #e8e8e8;
			padding: 5rpx 10rpx;
			border-radius: 5rpx;
		}
		
	}
</style>
